package com.example.nftapp;

import android.annotation.SuppressLint;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class VideoPlayerActivity extends AppCompatActivity {

    // 视频播放控制类
    private MediaPlayer mMediaPlayer;

    // VideoView用于播放视频
    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_video_player);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        videoView = findViewById(R.id.videoView);
        // 视频播放控制用到的组件
        ImageView ivPlayOrPause = findViewById(R.id.ivPlayOrPause);
        TextView tvTimeProgress = findViewById(R.id.tvTimeProgress);
        TextView tvTimeTotal = findViewById(R.id.tvTimeTotal);
        SeekBar sbVideoProgress = findViewById(R.id.sbVideoProgress);

        Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/raw/" + R.raw.big_buck_bunny);
        videoView.setVideoURI(videoUri);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @SuppressLint("DefaultLocale")
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mMediaPlayer = mediaPlayer;
                int videoDuration = mediaPlayer.getDuration();// 毫秒，时长60140毫秒
                //101秒 01:41
                int durationMiao = videoDuration / 1000;
                int minutes = durationMiao / 60;
                int seconds = durationMiao % 60;
                tvTimeTotal.setText(String.format("%02d:%02d", minutes, seconds));
            }
        });
        videoView.start();
        ivPlayOrPause.setImageResource(R.mipmap.play_btn_pause_prs);

        ivPlayOrPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击ivPlayOrPause 切换播放/暂停
                if (mMediaPlayer.isPlaying()) {
                    // 如果正在播放，那么点击后暂停
                    mMediaPlayer.pause();
                    ivPlayOrPause.setImageResource(R.mipmap.play_btn_play);
                } else {
                    // 如果未在播放，那么点击后播放
                    mMediaPlayer.start();
                    ivPlayOrPause.setImageResource(R.mipmap.play_btn_pause_prs);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (videoView != null) {
            videoView.suspend();
            videoView = null;
        }
    }
}